﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;

namespace StartTesting
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            try
            {
                string StartPath = @"C:\Users\Andrew Greene\Google Drive";
                //string StartPath = @"C:\Users\Andrew Greene\Google Drive\ENF Backup";

                treeView1.ImageList = imageList1;

                string iconKey = GetIconForFile(StartPath, imageList1);

                TreeNode newNode = new TreeNode(Path.GetFileName(StartPath));
                newNode.ImageKey = iconKey;
                newNode.SelectedImageKey = iconKey;
                AddDirectoryToTreeNode(StartPath, newNode);
                treeView1.Nodes.Add(newNode);

                newNode.Expand();
            }
            catch (Exception ex)
            {
                Debug.Assert(false, ex.Message);
            }
        }

        void AddDirectoryToTreeNode(string DirectoryPath, TreeNode RootNode)
        {
            foreach (string SubDirectoryPath in Directory.GetDirectories(DirectoryPath))
            {
                string iconKey = GetIconForFile(SubDirectoryPath, imageList1);

                string SubDirectoryName = Path.GetFileName(SubDirectoryPath);
                TreeNode newNode = new TreeNode(SubDirectoryName);
                newNode.ImageKey = iconKey;
                newNode.SelectedImageKey = iconKey;
                AddDirectoryToTreeNode(SubDirectoryPath, newNode);
                RootNode.Nodes.Add(newNode);
            }

            foreach (string FilePath in Directory.GetFiles(DirectoryPath))
            {
                string iconKey = GetIconForFile(FilePath, imageList1);

                string FileName = Path.GetFileName(FilePath);
                TreeNode newNode = new TreeNode(FileName);
                newNode.Tag = FilePath;
                newNode.ImageKey = iconKey;
                newNode.SelectedImageKey = iconKey;
                RootNode.Nodes.Add(newNode);
            }
        }

        string GetIconForFile(string filePath, ImageList imageList)
        {
            KeyValuePair<string, Image> kvp = GetIconForFile(filePath);
            if (!imageList.Images.ContainsKey(kvp.Key))
            {
                imageList.Images.Add(kvp.Key, kvp.Value);
            }

            return kvp.Key;
        }

        KeyValuePair<string, Image> GetIconForFile(string fileFullPath)
        {
            SHFILEINFO shinfo = new SHFILEINFO();
            IntPtr hImgSmall = Win32.SHGetFileInfo(fileFullPath, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_SMALLICON);
            System.Drawing.Icon myIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon);
            Image tempImage = myIcon.ToBitmap();

            return new KeyValuePair<string, Image>(shinfo.iIcon.ToString(), tempImage);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //string Url = @"file:///" + FilePath;
            try
            {
                //Environment.CurrentDirectory

                //Application.
                ProcessStartInfo info = new ProcessStartInfo();
                //info.FileName = Url;
                info.FileName = "cmd.exe";
                info.WorkingDirectory = "C:/";
                System.Diagnostics.Process.Start(info);
            }
            catch (Exception exception)
            {
                Debug.Assert(false, "There was a problem: " + exception.Message);
            }
        }

        private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            object tag = e.Node.Tag;
            if (tag != null && tag is string)
            {
                string path = tag as string;
                OpenFileInDefaultApp(path);
            }
        }

        public static void OpenFileInDefaultApp(string FilePath)
        {
            if (!File.Exists(FilePath))
            {
                Debug.Assert(false);
                return;
            }

            string Url = @"file:///" + FilePath;
            try
            {
                System.Diagnostics.Process.Start(Url);
            }
            catch (Exception exception)
            {
                Debug.Assert(false, "There was a problem: " + exception.Message);
            }
        }
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct SHFILEINFO
    {
        public IntPtr hIcon;
        public IntPtr iIcon;
        public uint dwAttributes;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
        public string szDisplayName;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
        public string szTypeName;
    };
    class Win32
    {
        public const uint SHGFI_ICON = 0x100;
        public const uint SHGFI_LARGEICON = 0x0; // 'Large icon
        public const uint SHGFI_SMALLICON = 0x1; // 'Small icon
        [DllImport("shell32.dll")]
        public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);
    }
}
